﻿#ifndef PARTICLE_TRACK_H
#define PARTICLE_TRACK_H

#include <random>

#include "MLObj.h"
#include "ParticleObj.h"
#include "Stream.h"

#include "Drawer.h"

class ParticleTrack {
public:
	ParticleTrack(
		int frame_size_x, int frame_size_y,
		double frame_rate,
		VelocityStream& stream,
		Color color,
		Byte alpha,
		double brightness,
		Double2 lifespan_range,
		Location radius_range,
		Double2 v_range,
		Double2 a_delta_range,
		Double2 init_angle_range,
		Double2 a_angle_range,
		GLuint glsl,
		GLuint mix_glsl,
		double particles_sparseness,
		int record_radius
	);
	void clear();
	void get_frame(size_t frame_index, MLFrame& frame);

private:
	size_t* _particles_num_list;

	int _frame_size_x, _frame_size_y;
	Color _color;
	
	std::uniform_int_distribution<int> _lifespan_range;
	std::uniform_int_distribution<int> _radius_range;
	std::uniform_real_distribution<double> _v_range;
	std::uniform_real_distribution<double> _a_delta_range;

	VelocityStream _stream;

	ParticleObj* _particle_pool; // 对象池
	size_t _allocated_particle_count = 0;
	size_t max_particles;

	void _draw_particle(size_t particles_num, MLFrame& frame);
	void _allocate_particles(int count);

	std::mt19937 _random_engine;
	double dt;

	std::vector<ParticleObj*> _active_particles;  // 活跃粒子
	std::vector<ParticleObj*> _inactive_particles; // 可重用粒子池

	ParticleSystem _p_system;
	GLuint _glsl;
	GLuint _mix_glsl;
};

#endif
